namespace Billing
{
    using Billing.Controls;
    using System;
    using System.Data;
    using System.Text.RegularExpressions;
    using System.Web.UI;
    using System.Web.UI.WebControls;


    public class ChangePassword : Page
    {
        protected Button BtnSave;
        protected Label lblUserName;
        protected TextBox txtConfirmNewPassword;
        protected TextBox txtNewPassword;
        protected TextBox txtOldPassword;
        protected ucHighlight ucHighlight1;
        protected ucPageTitle ucPageTitle1;
        protected UpdatePanel UpdatePanel4;
        protected UpdateProgress UpdateProgress1;

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            if (this.txtOldPassword.Text.Trim() == "")
            {
                this.ucHighlight1.ErrorMessage = "Invalid old password";
            }
            else if (this.txtNewPassword.Text.Trim() != this.txtConfirmNewPassword.Text.Trim())
            {
                this.ucHighlight1.ErrorMessage = "New password not match";
            }
            else if (this.txtOldPassword.Text.Trim() == this.txtNewPassword.Text.Trim())
            {
                this.ucHighlight1.ErrorMessage = "Old Password same as new password ";
            }
            else
            {
                ClsUserProfile cls = new ClsUserProfile {
                    LoginId = this.lblUserName.Text,
                    LoginPassword = ClsPasswordHelper.Encrypt(this.txtOldPassword.Text)
                };
                DataTable dt = cls.DoLogin();
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        if (this.txtNewPassword.Text.Trim() == "")
                        {
                            this.ucHighlight1.ErrorMessage = "Invalid new password, password cannot empty";
                        }
                        else if (this.txtNewPassword.Text.Trim() != this.txtConfirmNewPassword.Text.Trim())
                        {
                            this.ucHighlight1.ErrorMessage = "Invalid new password";
                        }
                        else
                        {
                            Regex match = new Regex(@"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,20}$");
                            if (!match.IsMatch(this.txtNewPassword.Text))
                            {
                                this.ucHighlight1.ErrorMessage = "Passwords must be at least eight (8) characters in length and contain both letters (uppercase and lowercase) and numbers";
                            }
                            else
                            {
                                match = new Regex(".*[a-z].*");
                                if (!match.IsMatch(this.txtNewPassword.Text))
                                {
                                    this.ucHighlight1.ErrorMessage = "Password must contain a-z";
                                }
                                else
                                {
                                    match = new Regex(".*[A-Z].*");
                                    if (!match.IsMatch(this.txtNewPassword.Text))
                                    {
                                        this.ucHighlight1.ErrorMessage = "Password must contain A-Z";
                                    }
                                    else
                                    {
                                        match = new Regex(".*[0-9].*");
                                        if (!match.IsMatch(this.txtNewPassword.Text))
                                        {
                                            this.ucHighlight1.ErrorMessage = "Password must contain 0-9";
                                        }
                                        else
                                        {
                                            cls.LoginPassword = ClsPasswordHelper.Encrypt(this.txtNewPassword.Text);
                                            cls.UpdateByLoginIdForPassword();
                                            this.ucHighlight1.Message = "Password successfully updated";
                                            this.BtnSave.Visible = false;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        this.ucHighlight1.ErrorMessage = "Invalid Old Password";
                    }
                }
                else
                {
                    this.ucHighlight1.ErrorMessage = "Invalid Old Password";
                }
            }
        }

        private void GetPermission()
        {
            ClsACLHelper cls = new ClsACLHelper();
            string pageName = this.Page.ToString().Substring(4, this.Page.ToString().Substring(4).Length - 5) + ".aspx";
            cls.SetPermission(LoginUser.TreeMenu, pageName);
            if (!cls.Read0)
            {
                base.Response.Redirect("logout.aspx", true);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session["BUID"] == null)
            {
                base.Response.Redirect("default.aspx", true);
            }
            this.lblUserName.Text = LoginUser.LoginID;
            this.GetPermission();
        }
    }
}
